import datetime

import matplotlib.pyplot as plt
import pandas as pd
import pymysql
import matplotlib.ticker as mticker
import matplotlib.dates as mdate

# 建立数据库连接
from pandas.plotting._matplotlib import timeseries
from matplotlib.patches import Rectangle

conn = pymysql.connect("localhost", "stockx", "stockx#2020", "stockx", charset="utf8")
print("连接成功")
# 读取数据库表数据
data = pd.read_sql("select * from  two_color_ball order by dt desc limit 20", con=conn)
# 数据转化为列表
x = list(data.dt)  # 日期
y0 = list(data.b1)
y1 = list(data.r1)
y2 = list(data.r2)
y3 = list(data.r3)
y4 = list(data.r4)
y5 = list(data.r5)
y6 = list(data.r6)


# 生成横纵坐标信息
# 配置横坐标
plt.gca().xaxis.set_major_formatter(mdate.DateFormatter('%m/%d/%Y'))
plt.gca().xaxis.set_major_locator(mdate.DayLocator())
# Plot
plt.xticks(pd.date_range(x[0], x[-1]), rotation=90)
plt.plot(x, y1)
#plt.gcf().autofmt_xdate()  # 自动旋转日期标记
#
#
# fig1 = plt.figure(figsize=(15,5))
# ax1 = fig1.add_subplot(1,1,1)
# ax1.xaxis.set_major_formatter(mdate.DateFormatter('%Y-%m-%d'))#设置时间标签显示格式
# plt.xticks(pd.date_range(x[0], x[-1]), rotation=90)

plt.show()


print(type(x))
# 关闭数据库连接
conn.close()
